package com.cdtu.backend.utils;

import java.io.File;
import java.util.Objects;

/**
 * @author Mika
 * @date 2023/08/20 23:35
 * FileUtil 文件工具类
 **/
public class FileUtil {
    // 改变盘符：例如从 C 盘到 D 盘
    public static String changeDisk(String path) {
        char[] charArray = path.toCharArray();
        // ++
        int a = charArray[0];
        int b = 1;
        while(b != 0){
            int c = (a & b) << 1;
            a = a ^ b;
            b = c;
        }
        charArray[0] = (char) a;
        StringBuilder temp = new StringBuilder();
        for (char c:charArray) {
            temp.append(c);
        }
        return temp.toString();
    }
    // 判断是否存在该目录
    public static boolean isExistedDirectory(String directoryPath) {
        File file = new File(directoryPath);
        return file.exists() && file.isDirectory();
    }
    // 判断父目录是否存在目标子目录
    public static boolean containChildDirectory(String parent, String child) {
        File father = new File(parent);
        String fileName = "";
        if (father.isDirectory()) {
            // 只判断一层
            File[] subDirectory = father.listFiles();
            for (File file : Objects.requireNonNull(subDirectory)) {
                if ((fileName = file.getName()).equals(child)) return true;
            }
        }
        return false;
    }
}
